package com.tanguy.user.ui.me.activity

import cn.sinata.xldutils_kotlin.activity.ImagePagerActivity
import com.tanguy.user.R
import com.tanguy.user.base.TabLayoutActivity
import com.tanguy.user.bean.EvaluateDriver
import com.tanguy.user.bean.EvaluateHotel
import com.tanguy.user.net.http.HttpManager
import com.tanguy.user.ui.me.adapter.EvaluateDriverAdapter
import com.tanguy.user.ui.me.adapter.EvaluateHotelAdapter
import com.tanguy.user.utlis.extend.loge
import com.tanguy.user.utlis.extend.requestSucceed
import org.jetbrains.anko.find
import org.jetbrains.anko.startActivity

/**
 * Created by dab on 2017/10/9 0009 15:03
 * 评价
 * type 1表示司机评价
 *      2表示用户评价
 */
class GradeActivity : TabLayoutActivity() {
    override fun getTabString(): ArrayList<String> = arrayListOf("司机评价", "酒店评价")
    private var type = 1
    private var isLoadHotel = false //第一次进入 默认加载司机的,如果不点酒店,就不加载酒店
    val driver = ArrayList<EvaluateDriver>()
    val hotel = ArrayList<EvaluateHotel>()
    private val evaluateDriverAdapter by lazy {
        EvaluateDriverAdapter(driver)
    }
    private val evaluateHotelAdapter by lazy {
        EvaluateHotelAdapter(hotel)
    }

    override fun onTabItemSelected(position: Int) {
        when (position) {
            0 -> {
                type = 1
                mRvContent.adapter = evaluateDriverAdapter.apply { setSwipeRefreshLayout(find(R.id.sr_refresh)) { loadData(it) } }
            }
            1 -> {
                type = 2
                if (!isLoadHotel) {
                    loadData(1)
                }
                isLoadHotel = true
                mRvContent.adapter = evaluateHotelAdapter.apply { setSwipeRefreshLayout(find(R.id.sr_refresh)) { loadData(it) } }
            }
        }
    }

    override fun initEvent() {
        super.initEvent()
        loadData(1)
        evaluateHotelAdapter.onImageClick = { list, position ->
            startActivity<ImagePagerActivity>("url" to list,"position" to position)
            loge("显示图片"+list.toString())
        }
    }

    private fun loadData(page: Int) {
        if (type == 1) {
            HttpManager.getDriverEvaList(page)
                    .requestSucceed(this) {
                        evaluateDriverAdapter.setLoadData(it)
                    }
        } else {
            HttpManager.getHotelEvaList(page)
                    .requestSucceed(this) {
                        evaluateHotelAdapter.setLoadData(it)
                    }
        }
    }
}
